from flask import Flask,render_template,make_response,request,jsonify
import json
from urllib import request
import urllib

app = Flask(__name__)

@app.route('/index')
def index():
	return render_template('index/index.html')

@app.route('/index/get_remote_data')
def get_remote_data():
	data = {"arguments":[
		{"conditions":"[]"},
		{"from":"0"},{"pagesize":"15"},
		{"sortField":"dynProperties.goodsPubTime"},
		{"sortType":"desc"}]}

	headers = {
		"Accept": "*/*", 
		"Accept-Encoding": "gzip, deflate",
		"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
		"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.101 Safari/537.36",
		"X-Requested-With": "XMLHttpRequest"
	}

	d1 = '{"arguments":[{"conditions":"[]"},{"from":"0"},{"pagesize":"15"},{"sortField":"dynProperties.goodsPubTime"},{"sortType":"desc"}]}'
	d2 = json.dumps(data) + ""
	d2 = d2.replace(' ','')
	print("d1:", d1, flush = True)
	print("d2:", d2, flush = True)

	post_url = 'https://www.greatgas.cn/prod/rest/goods/mall/searchByConditions'
	req = request.Request(post_url, d2.encode('utf-8'), headers)

	with request.urlopen(req) as f:
		rsp = f.read().decode("utf-8")
		print("json", rsp, flush = True)
		jsonobj = json.loads(rsp)
		return jsonify(jsonobj)

def test():
	pass;


if __name__ == '__main__':
	app.run()